﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using OpenData.WebApp;
using OpenData.Framework;

namespace OpenData.WebApp.Controllers
{
    public class DataSchemaController : Controller
    {

        // GET: DataSchema
        public ActionResult Index(string id)
        {
            string entityName = id;
            using (OpenDatabase db = new OpenDatabase("test"))
            {
                return View(db.Entity(entityName).Schema.QueryColumn());
            }
        }

        // GET: DataSchema/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            using (OpenDatabase db = new OpenDatabase("test"))
            {
                ShCol shCol = db.Entity(entityName).Query().First(.Find(id);
                if (shCol == null)
                {
                    return HttpNotFound();
                }
                return View(shCol);
            }
        }

        // GET: DataSchema/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: DataSchema/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,Name,Label,ControlType,AllowNull,Length,Order,Modifiable,Indexable,ShowInGrid,Tooltip,SelectionFolder,DefaultValue,Summarize,IsSystemField")] ShCol shCol)
        {
            if (ModelState.IsValid)
            {
                using (OpenDatabase db = new OpenDatabase("test"))
                {
                    db.Coulumns.Add(shCol);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }

            return View(shCol);
        }

        // GET: DataSchema/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            using (OpenDatabase db = new OpenDatabase("test"))
            {
                ShCol shCol = db.Coulumns.Find(id);
                if (shCol == null)
                {
                    return HttpNotFound();
                }
                return View(shCol);
            }
        }

        // POST: DataSchema/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,Name,Label,ControlType,AllowNull,Length,Order,Modifiable,Indexable,ShowInGrid,Tooltip,SelectionFolder,DefaultValue,Summarize,IsSystemField")] ShCol shCol)
        {
            if (ModelState.IsValid)
            {
                using (OpenDatabase db = new OpenDatabase("test"))
                {
                    db.Entry(shCol).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            return View(shCol);
        }

        // GET: DataSchema/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            using (OpenDatabase db = new OpenDatabase("test"))
            {
                ShCol shCol = db.Coulumns.Find(id);
                if (shCol == null)
                {
                    return HttpNotFound();
                }
                return View(shCol);
            }
        }

        // POST: DataSchema/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            using (OpenDatabase db = new OpenDatabase("test"))
            {
                ShCol shCol = db.Coulumns.Find(id);
                db.Coulumns.Remove(shCol);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                 
            }
            base.Dispose(disposing);
        }
    }
}
